﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GPSTrackingClientsWebsiteManager.Espanol.Pages
{
    public partial class ImeiList : Telerik.Web.UI.RadAjaxPage
    {
        private void GetImeiByUsername(string Username)
        {
            GPSTrackingServerDB.Entities.TList<GPSTrackingServerDB.Entities.Imei> imeiEntities = GPSTrackingServerDBCustom.Data.DataRepository.ImeiProvider.GetImeiByUsername(Username);
            foreach (GPSTrackingServerDB.Entities.Imei entity in imeiEntities)
            {
                entity.EntityState = GPSTrackingServerDB.Entities.EntityState.Unchanged;
            }

            GPSTrackingServerDB.Data.DataRepository.ImeiProvider.DeepLoad(imeiEntities,true,GPSTrackingServerDB.Data.DeepLoadType.IncludeChildren, typeof(GPSTrackingServerDB.Entities.GpsModels));
            
            ImeiGrid.DataSource = imeiEntities;
        }

        private void GetAllImeiType()
        {
            cmbImeiType.DataSource = GPSTrackingServerDB.Data.DataRepository.ImeiTypeProvider.GetAll();
            cmbImeiType.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {                
                GetAllImeiType();

                if (Request.Params["Username"] != null)
                {
                    GetImeiByUsername(Request.QueryString["Username"]);
                    ImeiGrid.DataBind();

                    //Hide column                    
                    ImeiGrid.MasterTableView.GetColumnSafe("Description").Display = false;
                    ImeiGrid.MasterTableView.GetColumnSafe("ImeiTypeId").Display = false;
                    ImeiGrid.MasterTableView.GetColumnSafe("GpsModelId").Display = false;
                }
            }
        }

        protected void ImeiGrid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            GetImeiByUsername(Request.QueryString["Username"]);
        }        

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                GPSTrackingServerDB.Entities.Imei imeiEntity = new GPSTrackingServerDB.Entities.Imei();
                imeiEntity.Imei = HiddenField2.Value;
                imeiEntity.Name = txtName.Text;
                imeiEntity.SimNumber = Convert.ToDecimal(txtSimNumber.Text);
                imeiEntity.Description = txtDescription.Text;
                imeiEntity.ImeiTypeId = Convert.ToInt32(cmbImeiType.SelectedValue);
                imeiEntity.GpsModelId = Convert.ToInt16(HiddenField1.Value);

                imeiEntity.OriginalImei = imeiEntity.Imei;

                GPSTrackingServerDB.Data.DataRepository.ImeiProvider.Update(imeiEntity);

                btnSave.Enabled = true;
                RadNotification1.Show();

            }
            catch (Exception ex)
            {

                GPSTrackingLogCustom.WebsiteLog.Error(ex.Message);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowErrorCatch", "show_miniError('" + ex.Message + "','GPS error ...');", true);
            }            
        }
    }
}